//
//  ExcellentViewController.m
//  Courses
//
//  Created by YiXuePxh on 15/5/19.
//  Copyright (c) 2015年 boyibang. All rights reserved.
//

#import "ExcellentViewController.h"
#import "ExcellentClass.h"
#import "WTFRect.h"
#import "ExcellentTableViewCell.h"
#import "ExcellentDetailVC.h"
@interface ExcellentViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation ExcellentViewController{
    NSMutableArray* _excellList;
}
SYNTHESIZE_SINGLETON_FOR_IMPL(ExcellentViewController)
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tabBarController.tabBar setHidden:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    UITableView* table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, screenSize.height)];
    table.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:table];
    table.delegate = self;
    table.dataSource = self;
    //获取加载的数据
    NSString* jsonPath = [[NSBundle mainBundle]pathForResource:@"moban" ofType:@"json"];
    NSData* jsonData = [NSData dataWithContentsOfFile:jsonPath];
    NSString* jsonStr = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSLog(@"jsonString:%@",jsonStr);
    id json = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
    NSLog(@"json:%@",json);
    _excellList = [[ExcellentClass getExcellentDataWithJsonDict:json[@"boutique"]] copy];
    NSLog(@"_excellList:%lu",(unsigned long)_excellList.count);
    //数据获取结束
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _excellList.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return AutoFloat(80);
}
-(UITableViewCell* )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ExcellentTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[ExcellentTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    if (_excellList.count > indexPath.row) {
        cell.excellent = _excellList[indexPath.row];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"tableview:%ld",(long)indexPath.row);
    ExcellentClass* class = _excellList[indexPath.row];
    UIBarButtonItem* backItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:nil action:nil];
    [self.navigationItem setBackBarButtonItem:backItem];
    ExcellentDetailVC* detailVC = [[ExcellentDetailVC alloc]initWithJsonDict:class.classList ClassName:class.className];
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
